package com.xiaochong.bitong.api.controller;

import com.xiaochong.bitong.api.constant.HttpHeaderCons;
import com.xiaochong.bitong.api.service.KActionDetailService;
import com.xiaochong.bitong.api.utils.ApiVersion;
import com.xiaochong.bitong.api.utils.RequestUtil;
import com.xiaochong.tir.common.data.hbasepo.TradesToKLine;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.data.po.KLine;
import com.xiaochong.tir.common.data.vo.*;
import com.xiaochong.tir.common.data.vo.app.AppFundsFlowVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: K线详情页面
 * @Author: org
 * @CreateDate: 2018/8/10 11:50
 */
@Api(value = "k线详情页面")
@RestController
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/KActionDetail")
public class KActionDetailController {
    @Autowired
    private KActionDetailService kActionDetailService;


    /**
     * k线数据-K线图根据传过来的时间戳和num数量查询相应数量k线数据（时间降序）
     * @param LongTime
     * @param exchangeId
     * @param transactionName
     * @param num
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "k线数据-K线图按照最后时间戳和条数查询" , notes = "k线数据-K线图")
    @PostMapping("kActionLongTime")
    public ApiResultVo<List<TradesToKLine>> kActionLongTime(@RequestParam(value = "LongTime", required = false) String LongTime, @RequestParam(value = "exchangeId", required = false) String exchangeId, @RequestParam(value = "transactionName", required = false) String transactionName
            , @RequestParam(value = "num", required = false) Integer num) throws Exception {
        return  kActionDetailService.kActionLongTime(LongTime,exchangeId,transactionName,num);
    }

    /**
     * k线数据-K线图-两天数据
     * @param exchangeId
     * @param transactionName
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "k线数据-K线图-两天数据" , notes = "k线数据-K线图-两天数据")
    @PostMapping("kActionOfTwoDay")
    public ApiResultVo<List<TradesToKLine>> kActionOfTwoDay(@RequestParam(value = "exchangeId", required = false) String exchangeId,@RequestParam(value = "transactionName", required = false) String transactionName) throws Exception {
        return kActionDetailService.kActionOfTwoDay(exchangeId,transactionName);
    }

    @ApiOperation(value = "k线数据-K线图-5/15、1h等维度数据" , notes = "k线数据-K线图-多维度数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exchangeId", value = "交易所id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "参数：5M/15M/30M/1H/4H/1D/1W/1MON", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "transactionName", value = "交易对", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("kActionByPart")
    public ApiResultVo<List<TradesToKLine>> kActionByPart(@RequestParam(value = "exchangeId", required = false) String exchangeId,
                                                          @RequestParam(value = "transactionName", required = false) String transactionName,
                                                          @RequestParam(value = "type", required = false) String type) throws Exception {
        return kActionDetailService.kActionByPart(exchangeId,transactionName,type);
    }
    /**
     * k线数据-K线图最新一次数据
     * @param exchangeId
     * @param transactionName
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "k线数据-K线图最新一次数据(暂时无用，请用websocket接口)" , notes = "k线数据-K线图最新一次数据（最多两天数据，当前分钟和下一分钟数据，一条时候是当前分钟数据）")
    @PostMapping("NewkAction")
    public ApiResultVo<List<TradesToKLine>> NewkAction(@RequestParam(value = "exchangeId", required = false) String exchangeId,@RequestParam(value = "transactionName", required = false) String transactionName) throws Exception {
        return  kActionDetailService.NewkAction(exchangeId,transactionName);
    }

    /**
     * 获取交易所列表
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "交易所列表" , notes = "交易所列表（参数：交易所id，交易对名称，维度）")
    @PostMapping("transactions")
    public ApiResultVo transactions() throws Exception {
        return kActionDetailService.getTransactions();
    }
    //k线数据-K线图
    @ApiOperation(value = "根据交易所获取纬度列表" , notes = "根据交易所获取纬度列表（参数：交易所id）")
    @PostMapping("dimensions")
    public ApiResultVo dimensions(@RequestParam(value = "exchangeId", required = false) Integer exchangeId) throws Exception {
        return kActionDetailService.getDimensions(exchangeId);
    }

    //k线数据-K线图大纬度
    @ApiOperation(value = "k线数据-K线图大纬度" , notes = "k线数据-K线图大纬度(日k数据接口)")
    @PostMapping("daykline")
    public ApiResultVo<KLine> daykline(@RequestParam(value = "exchangeId") Integer exchangeId, @RequestParam(value = "transactionName") String transactionName) throws Exception {
        return kActionDetailService.getDayLine(exchangeId,transactionName);
    }
    /**
     * k线数据-历史交易
     */
    @ApiOperation(value = "k线数据-历史交易(暂时无用，请用websocket接口)" , notes = "k线数据-历史交易(时时数据)")
    @PostMapping("tranes")
    public ApiResultVo tranes(@RequestParam(value = "exchangeId") Integer exchangeId,@RequestParam(value = "transactionName") String transactionName) {
        return kActionDetailService.getTranes(exchangeId,transactionName);
    }

    //历史交易列表
    @ApiOperation(value = "历史交易列表" , notes = "历史交易列表（第一次加载）")
    @PostMapping("tranesList")
    public ApiResultVo<ApiTradesHistoryVo> tranesList(@RequestParam(value = "exchangeId") Integer exchangeId,@RequestParam(value = "transactionName") String transactionName) throws Exception {
        return kActionDetailService.getTranesList(exchangeId,transactionName);
    }

    @ApiOperation(value = "表头请求数据(暂时无用，请用websocket接口)" , notes = "表头请求数据")
    @PostMapping("rise")
    public ApiResultVo rise(@RequestParam(value = "exchangeId") Integer exchangeId,@RequestParam(value = "transactionName") String transactionName) throws Exception {
        return kActionDetailService.getRise(exchangeId,transactionName);
    }
    @ApiOperation(value = "成交大单监控", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；3000-参数错误")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "number", value = "查询数量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "exchangeId", value = "交易所id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "symbol", value = "交易对", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/historyTradesMax")
    public ApiResultVo<List<ApiMaxTradesVo>> historyTradesMax(@RequestParam(value = "number") Integer number, @RequestParam(value = "exchangeId") Integer exchangeId, @RequestParam(value = "symbol") String symbol){
        return kActionDetailService.historyTradesMax(number,exchangeId,symbol);
    }
    @ApiOperation(value = "资金流入今日、5、10、一小时、24小时情况", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；3000-参数错误")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exchangeId", value = "交易所id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "symbol", value = "交易对", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "timePart", value = "3h/1d", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/capitalInflowSituation")
    public ApiResultVo<ApiCapitalInflowSituationDataVo> capitalInflowSituation(@RequestParam(value = "exchangeId") Integer exchangeId, @RequestParam(value = "symbol") String symbol, @RequestParam(value = "timePart") String timePart){
        return kActionDetailService.capitalInflowSituation(exchangeId,symbol,timePart);
    }
    @ApiOperation(value = "k线行情-查询交易对是否自选(token需要在header中传入)", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；3000-参数错误,返回参数selfSelectFlag，1是组选，2不是自选")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "middleId", value = "中间表id", required = true, dataType = "int", paramType = "query")
    })
    @PostMapping("/validateSymbolSelfSelect"+ HttpHeaderCons.AUTH_URL)
    public ApiResultVo validateSymbolSelfSelect(@RequestParam(value = "middleId") Integer middleId,HttpServletRequest request){
        String token = RequestUtil.getHeaderName(request  , HttpHeaderCons.TOKEN);
        return kActionDetailService.validateSymbolSelfSelect(middleId,token);
    }
    @ApiOperation(value = "5日资金流入柱状图", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；3000-参数错误")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "middleId", value = "中间表id", required = true, dataType = "int", paramType = "query")
    })
    @PostMapping("/capitalInflowSituationOfFiveDay")
    public ApiResultVo<AppFundsFlowVo> capitalInflowSituationOfFiveDay(@RequestParam(value = "middleId") Integer middleId){
        return kActionDetailService.capitalInflowSituationOfFiveDay(middleId);
    }
    @ApiOperation(value = "深度请求数据(暂时无用，请用websocket接口)" , notes = "深度请求数据")
    @PostMapping("MakeDepth")
    public ApiResultVo MakeDepth(@RequestParam(value = "exchangeId") Integer exchangeId,@RequestParam(value = "transactionName") String transactionName) throws Exception {
        return kActionDetailService.getDepth(exchangeId,transactionName);
    }
    @ApiOperation(value = "获取货币数据)", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；3000-参数错误,返回参数selfSelectFlag，1是组选，2不是自选")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "middleId", value = "中间表id", required = true, dataType = "int", paramType = "query")
    })
    @PostMapping("/getCurrencyInfo")
    public ApiResultVo<CurrencyInfo> getCurrencyInfo(@RequestParam(value = "middleId") Integer middleId, HttpServletRequest request){
        return kActionDetailService.getCurrencyInfo(middleId);
    }
}
//k线数据-K线图
   /* @ApiOperation(value = "根据交易所获取交易对列表" , notes = "根据交易所获取交易对列表（参数：交易所id）")
    @PostMapping("allExchange")
    public ApiResultVo allExchange(@RequestParam(value = "exchangeId", required = false) int exchangeId) throws Exception {
        ApiResultVo resultVo = new ApiResultVo();
        try {
            ExchangeTransactionpairMiddle exchangeTransactionpairMiddle=new ExchangeTransactionpairMiddle();
            exchangeTransactionpairMiddle.setEnableFlag( true );
            exchangeTransactionpairMiddle.setExchangeId( exchangeId );
            List<ExchangeTransactionpairMiddle> exchangeMiddleList = exchangeTransactionpairMiddleMapper.selectByEntityList( exchangeTransactionpairMiddle );
            resultVo.setCode( ApiResultCodeUtils.BASE_SUCCESS.getCode());
            resultVo.setMessage(ApiResultCodeUtils.BASE_SUCCESS.getMsg());
            resultVo.setTradeId(LogTrace.getTrace().getTraceId());
            resultVo.setData( exchangeMiddleList );
            resultVo.setTotal( exchangeMiddleList.size() );
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("k线数据-K线图", e);
        }
        return resultVo;
    }*/